package com.cherrish.demo.thread;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author cherrish
 * @time 2019-02-27 14:17
 * @name TestThread4
 * @desc:
 */
public class TestThread4 {
    public static void main(String[] args) throws InterruptedException {
        java.util.concurrent.ExecutorService threadPoolExecutor =
                java.util.concurrent.Executors.newCachedThreadPool();
        Future<Integer> future = threadPoolExecutor.submit(
                new Callable<Integer>() {
                    @Override
                    public Integer call() throws Exception {
// 返回一个值或受检查的异常
//throw new Exception();
                        return new Random().nextInt(100);
                    }
                });
/**
  * 如果不再需要新任务，请适当关闭threadPoolExecutor并拒绝新任务
  */
        threadPoolExecutor.shutdown();
/**
  * 模拟其他业务逻辑
  */
        Thread.sleep(1000);
//Integer result = future.get(0, TimeUnit.SECONDS);
        Integer result = null;
        try {
            result = future.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("result========" + result);
    }
}
